import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as nodemailer from 'nodemailer';

@Injectable()
export class EmailService {
  private transporter: nodemailer.Transporter;
  private readonly logger = new Logger(EmailService.name);

  constructor(private configService: ConfigService) {
    this.transporter = nodemailer.createTransport({
      host: process.env.EMAIL_HOST || 'smtp.qq.com',
      port: parseInt(process.env.EMAIL_PORT || '465'),
      secure: true, // 使用 SSL
      auth: {
        user: process.env.EMAIL_USER || '1123654054@qq.com',
        pass: process.env.EMAIL_PASSWORD || 'yyexqwmfsdorhicb',
      },
    });

    this.logger.log('邮箱服务初始化成功');
  }

  /**
   * 发送验证码邮件
   */
  async sendVerificationCode(to: string, code: string): Promise<boolean> {
    try {
      const from = process.env.EMAIL_FROM || '开发者导航 <1123654054@qq.com>';
      
      await this.transporter.sendMail({
        from,
        to,
        subject: '【开发者导航】邮箱验证码',
        html: `
          <div style="padding: 20px; background-color: #f5f5f5;">
            <div style="max-width: 600px; margin: 0 auto; background-color: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
              <h2 style="color: #333; margin-bottom: 20px;">邮箱验证码</h2>
              <p style="color: #666; font-size: 16px; line-height: 1.6;">您好！</p>
              <p style="color: #666; font-size: 16px; line-height: 1.6;">您的验证码是：</p>
              <div style="text-align: center; margin: 30px 0;">
                <span style="display: inline-block; padding: 15px 40px; font-size: 32px; font-weight: bold; color: #4F46E5; background-color: #EEF2FF; border-radius: 8px; letter-spacing: 8px;">
                  ${code}
                </span>
              </div>
              <p style="color: #999; font-size: 14px;">验证码5分钟内有效，请勿泄露给他人。</p>
              <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;" />
              <p style="color: #999; font-size: 12px; text-align: center;">
                这是一封自动发送的邮件，请勿回复。<br />
                © 2025 开发者导航 DevToolsHub
              </p>
            </div>
          </div>
        `,
      });

      this.logger.log(`验证码邮件已发送至: ${to}`);
      return true;
    } catch (error) {
      this.logger.error(`发送邮件失败: ${error.message}`, error.stack);
      return false;
    }
  }

  /**
   * 发送欢迎邮件
   */
  async sendWelcomeEmail(to: string, nickname: string): Promise<boolean> {
    try {
      const from = process.env.EMAIL_FROM || '开发者导航 <1123654054@qq.com>';
      
      await this.transporter.sendMail({
        from,
        to,
        subject: '欢迎加入开发者导航！',
        html: `
          <div style="padding: 20px; background-color: #f5f5f5;">
            <div style="max-width: 600px; margin: 0 auto; background-color: white; padding: 30px; border-radius: 10px;">
              <h2 style="color: #333;">欢迎加入开发者导航！</h2>
              <p style="color: #666; line-height: 1.8;">
                Hi ${nickname}，<br><br>
                感谢您注册开发者导航！我们致力于为开发者提供最全面的开发工具导航服务。<br><br>
                立即开始探索：<br>
                • 发现优质开发工具<br>
                • 收藏喜欢的工具<br>
                • 参与工具评价<br><br>
                祝您使用愉快！
              </p>
              <div style="text-align: center; margin-top: 30px;">
                <a href="${process.env.APP_URL || 'http://localhost:3000'}" 
                   style="display: inline-block; padding: 12px 30px; background-color: #4F46E5; color: white; text-decoration: none; border-radius: 6px;">
                  开始探索
                </a>
              </div>
            </div>
          </div>
        `,
      });

      this.logger.log(`欢迎邮件已发送至: ${to}`);
      return true;
    } catch (error) {
      this.logger.error(`发送欢迎邮件失败: ${error.message}`, error.stack);
      return false;
    }
  }
}

